package com.gwd.task.model;

import cn.hutool.core.bean.BeanUtil;
import com.gwd.task.entity.GroupEntity;
import com.gwd.task.group.GroupInsert;
import com.gwd.task.group.GroupUpdate;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhiyongming
 * @date 2020/11/6 16:09
 */
@Data
@NoArgsConstructor
public class GroupDTO {
    @NotNull(message = "组别id不能为空", groups = {GroupUpdate.class})
    @Null(message = "组别id不能传值", groups = {GroupInsert.class})
    private Long groupId;
    @NotBlank(message = "组别名称不能为空", groups = {GroupInsert.class})
    private String groupName;
    @NotNull(message = "组长id不能为空", groups = {GroupInsert.class})
    private Long groupLeader;
    private Integer remove;
    private String leaderName;
    private String members;//组内成员
    private List<Long> membersList;

    public GroupDTO(GroupEntity entity) {
        BeanUtil.copyProperties(entity, this);
        if (entity.getMembers()!=null && !entity.getMembers().equals("")) {
            String[] s = entity.getMembers().split(",");
            this.membersList = new ArrayList<>();
            for (String s1 : Arrays.stream(s).collect(Collectors.toList())) {
                this.membersList.add(Long.valueOf(s1));
            }
        }
    }
}
